package com.ruoyi.business.controller;

import com.ruoyi.business.domain.EquipmentTypes;
import com.ruoyi.business.dto.EquipmentTypeTreeQueryDTO;
import com.ruoyi.business.service.IEquipmentTypesService;
import com.ruoyi.business.vo.DeviceVariableStatisticsVO;
import com.ruoyi.business.vo.EquipmentTypeTreeVO;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 存储设备类型信息的，支持树状结构Controller
 * 
 * @author ldkj
 * @date 2025-02-27
 */
@Api(tags = "设备类型管理")
@RestController
@RequestMapping("/deviceInfo/types")
public class EquipmentTypesController extends BaseController
{
    @Autowired
    private IEquipmentTypesService equipmentTypesService;

    /**
     * 查询存储设备类型信息的，支持树状结构列表
     */
    //@ApiOperation("查询设备类型列表")
    //@PreAuthorize("@ss.hasPermi('deviceInfo:types:list')")
    @GetMapping("/list")
    @Anonymous
    public AjaxResult list(EquipmentTypes equipmentTypes)
    {
        List<EquipmentTypes> list = equipmentTypesService.selectEquipmentTypesList(equipmentTypes);
        return success(list);
    }

    /**
     * 导出存储设备类型信息的，支持树状结构列表
     */
    @ApiOperation("导出设备类型列表")
    @PreAuthorize("@ss.hasPermi('deviceInfo:types:export')")
    @Log(title = "存储设备类型信息的，支持树状结构", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EquipmentTypes equipmentTypes)
    {
        List<EquipmentTypes> list = equipmentTypesService.selectEquipmentTypesListForExport(equipmentTypes);
        ExcelUtil<EquipmentTypes> util = new ExcelUtil<EquipmentTypes>(EquipmentTypes.class);
        util.exportExcel(response, list, "存储设备类型信息的，支持树状结构数据");
    }

    /**
     * 导入存储设备类型信息的，支持树状结构数据
     */
    @ApiOperation("导入设备类型数据")
    @PreAuthorize("@ss.hasPermi('deviceInfo:types:import')")
    @Log(title = "存储设备类型信息的，支持树状结构", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(@ApiParam("Excel文件") MultipartFile file, @ApiParam("是否更新支持") boolean updateSupport) throws Exception
    {
        try
        {
            ExcelUtil<EquipmentTypes> util = new ExcelUtil<EquipmentTypes>(EquipmentTypes.class);
            List<EquipmentTypes> equipmentTypesList = util.importExcel(file.getInputStream());
            String operName = getUsername();
            String message = equipmentTypesService.importEquipmentTypes(equipmentTypesList, updateSupport, operName);
            return success(message);
        }
        catch (Exception e)
        {
            return error("导入失败：" + e.getMessage());
        }
    }

    /**
     * 下载导入模板
     */
    @ApiOperation("下载设备类型导入模板")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<EquipmentTypes> util = new ExcelUtil<EquipmentTypes>(EquipmentTypes.class);
        util.importTemplateExcel(response, "设备类型数据");
    }

    /**
     * 获取存储设备类型信息的，支持树状结构详细信息
     */
    @ApiOperation("获取设备类型详细信息")
    @PreAuthorize("@ss.hasPermi('deviceInfo:types:query')")
    @GetMapping(value = "/{equipmentTypeId}")
    public AjaxResult getInfo(@ApiParam("设备类型ID") @PathVariable("equipmentTypeId") Long equipmentTypeId)
    {
        return success(equipmentTypesService.selectEquipmentTypesByEquipmentTypeId(equipmentTypeId));
    }

    /**
     * 新增存储设备类型信息的，支持树状结构
     */
    @ApiOperation("新增设备类型")
    @PreAuthorize("@ss.hasPermi('deviceInfo:types:add')")
    @Log(title = "存储设备类型信息的，支持树状结构", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@ApiParam("设备类型信息") @RequestBody EquipmentTypes equipmentTypes)
    {
        return toAjax(equipmentTypesService.insertEquipmentTypes(equipmentTypes));
    }

    /**
     * 修改存储设备类型信息的，支持树状结构
     */
    @ApiOperation("修改设备类型")
    @PreAuthorize("@ss.hasPermi('deviceInfo:types:edit')")
    @Log(title = "存储设备类型信息的，支持树状结构", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@ApiParam("设备类型信息") @RequestBody EquipmentTypes equipmentTypes)
    {
        return toAjax(equipmentTypesService.updateEquipmentTypes(equipmentTypes));
    }

    /**
     * 删除存储设备类型信息的，支持树状结构
     */
    @ApiOperation("删除设备类型")
    @PreAuthorize("@ss.hasPermi('deviceInfo:types:remove')")
    @Log(title = "存储设备类型信息的，支持树状结构", businessType = BusinessType.DELETE)
	@DeleteMapping("/{equipmentTypeIds}")
    public AjaxResult remove(@ApiParam("设备类型ID数组") @PathVariable Long[] equipmentTypeIds)
    {
        return toAjax(equipmentTypesService.deleteEquipmentTypesByEquipmentTypeIds(equipmentTypeIds));
    }

    /**
     * 根据设备类型名称查询设备类型树状结构（包含所有子类和关联的设备变量）
     */
    @ApiOperation("查询设备类型树状结构")
    @Anonymous
    @PostMapping("/query/tree")
    public AjaxResult getEquipmentTypeTree(@ApiParam("查询参数") @RequestBody EquipmentTypeTreeQueryDTO queryDTO)
    {
        EquipmentTypeTreeVO treeVO = equipmentTypesService.selectEquipmentTypeTreeByName(queryDTO.getEquipmentTypeName());
        if (treeVO == null) {
            return error("设备类型 \"" + queryDTO.getEquipmentTypeName() + "\" 不存在或没有关联数据");
        }
        return success(treeVO);
    }

    /**
     * 根据设备类型名称查询其下设备平均值
     */
    @ApiOperation("根据设备类型名称查询其下设备平均值")
    @Anonymous
    @PostMapping("/query/avg")
    public AjaxResult getEquipmentTypeAvg(@ApiParam("查询参数") @RequestBody EquipmentTypeTreeQueryDTO queryDTO)
    {
        DeviceVariableStatisticsVO deviceVariableStatisticsVO = equipmentTypesService.selectEquipmentTypeAvgByName(queryDTO.getEquipmentTypeName());
        if (deviceVariableStatisticsVO == null) {
            return error("设备类型 \"" + queryDTO.getEquipmentTypeName() + "\" 不存在或没有关联数据");
        }
        return success(deviceVariableStatisticsVO);
    }


    /**
     * 查询设备数统计信息列表
     * @return
     */
    @ApiOperation("查询设备数统计信息列表")
    @Anonymous
    @GetMapping("/getStatistics")
    public TableDataInfo getEquipmentStatistics()
    {
        return getDataTable(equipmentTypesService.getEquipmentStatistics());
    }

}
